package com.bawei.play.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.VideoEntity
import com.bawei.image.ImageOptions
import com.bawei.image.ImageStrategy
import com.bawei.image.ImgLoader
import com.bawei.play.databinding.ItemVideoLayoutBinding
import com.shuyu.gsyvideoplayer.GSYVideoManager

class VideoPlayAdapter(private val list: MutableList<VideoEntity>): RecyclerView.Adapter<VideoPlayAdapter.VideoPlayViewHolder>(){

    private var itemClickListener: OnItemClickListener? = null

    //图片框架
    private var imgLoader: ImgLoader = ImgLoader.Builder.setImageStrategy(ImageStrategy.GLIDE).build()

    //圆形图片参数
    private var imgCircleOption: ImageOptions = ImageOptions.Builder
        .setImageSize(intArrayOf(80, 80))
        .setCircleImage(true)
        .setErrorImage(com.bawei.common.R.drawable.ic_logo)
        .setPlaceImage(com.bawei.common.R.drawable.ic_logo)
        .skipMemoryCache(true)
        .build()

    //设置Item点击事件
    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickListener = listener
    }

    inner class VideoPlayViewHolder(val dataBinding: ItemVideoLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoPlayViewHolder {
       return VideoPlayViewHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: VideoPlayViewHolder, position: Int) {
        var entity = list[position]

        //视频播放器
        val player = holder.dataBinding.gsyPlayer
        player.setUp(entity.videopath, true, "")
        player.backButton.visibility = View.GONE
        player.fullscreenButton.visibility = View.GONE

        //头像
        var headImage = holder.dataBinding.ivHead
        imgLoader.loadUrlIntoImageView(holder.dataBinding.root.context, entity.avatar_url, headImage, imgCircleOption)
        //评论次数
        holder.dataBinding.tvPingLunCount.text = if (entity.commentnum > 0) entity.commentnum.toString() else "1.2W"
        //播放碟片
        var playImage = holder.dataBinding.ivMusicPlayer
        imgLoader.loadUrlIntoImageView(holder.dataBinding.root.context, entity.avatar_url, playImage, imgCircleOption)
        //属性动画
        val animator = ObjectAnimator.ofFloat(playImage, "Rotation", 0f, 360f)
        animator.duration = 1500 //动画时长
        animator.repeatCount = ValueAnimator.INFINITE //无线循环
        animator.interpolator = LinearInterpolator() //匀速效果
        animator.start()

        //添加子View点击事件
        holder.dataBinding.ivHead.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivAdd.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivDianZanBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivPingLunBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivShouCangBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivShareBtn.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivClose.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
        holder.dataBinding.ivSend.setOnClickListener {
            itemClickListener?.onItemChildClick(it, position)
        }
    }


    /**
     * 点击事件接口类
     */
    interface OnItemClickListener {
        fun onItemChildClick(v: View?, position: Int)
    }

    override fun getItemViewType(position: Int): Int {
        return 0
    }


}